﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business;
using NHibernate;
using ZLCMS.Data.Model;
using NHibernate.Criterion;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.TranferParam;
using ZLCMS.Business.Util;
using DotNet.Common.TransferParam;

namespace ZLCMS.Web.SystemManage.Basics.Param.Memory
{
    public partial class List : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        protected List<ParamKeyValuePair> _keyValueList;

        protected string _groupcode;
        protected string _groupname; 

        protected void Page_Load(object sender, EventArgs e)
        {
            //初始化全局变量值
            _groupcode = Request["groupcode"] == null ? "" : Request["groupcode"];
            _groupname = Request["groupname"] == null ? "" : Request["groupname"];
            _keyValueList = ParamKit.GetValueList("TranferParam", null).FindAll(SetupCondition);

            //初始化控件值
            tb_search_groupcode.Text = _groupcode;
            tb_search_groupname.Text = _groupname;

            //初始化DWZ分页控件
            _framePage = new FrameHelper(this) { RecordCount = GetListCount() };

            if (_framePage.IsAjaxPagerPostBack)
            {
                //分页返回
                BindList(_framePage.PageIndex, _framePage.PageSize);
            }
            else
            {
                //第一次进入页面
                BindList(1, _framePage.PageSize);
            }
        }

        /// <summary>
        /// 填充网格数据
        /// </summary>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">分页大小</param>
        protected void BindList(int pageIndex, int pageSize)
        {
            IList<ParamKeyValuePair> list = new List<ParamKeyValuePair>();
            for (int i = (pageIndex - 1) * pageSize; i < pageSize*pageIndex && i<_keyValueList.Count; i++ )
            {
                ParamKeyValuePair item = _keyValueList[i];
                list.Add(item);
            }
            //int count = pageIndex * pageSize < _keyValueList.Count ? pageIndex * pageSize : _keyValueList.Count - ((pageIndex - 1) * pageSize);
            // int startIndex = (pageIndex - 1) * pageSize < _keyValueList.Count ? (pageIndex - 1) * pageSize : _keyValueList.Count - count;

            grid.DataSource = list;//_keyValueList.GetRange(startIndex, count);
            grid.DataBind();
        }

        /// <summary>
        /// 查询条件
        /// </summary>
        private bool SetupCondition(ParamKeyValuePair item)
        {
            if (item.Key.Contains(_groupcode) && item.Key.Contains(_groupname))
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 查询数据总数
        /// </summary>
        /// <returns></returns>
        protected int GetListCount()
        {
            return _keyValueList.Count;
        }
    }
}
